package com.ruoyi.service.impl;

import com.ruoyi.entity.JrVersion;
import com.ruoyi.mapper.VersionMapper;
import com.ruoyi.service.VersionService;
import com.ruoyi.util.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class VersionServiceImpl implements VersionService {
    @Autowired
    private VersionMapper versionMapper;

    @Autowired
    private MinioUtil minioUtil;
    @Override
    public List<JrVersion> getVersionsByFileId(Long fileId,String fileName) {
//        数据库版本信息
        List<JrVersion> jrVersion = versionMapper.selectVersionsByFileId(fileId);
//        minio版本信息
        List<JrVersion> versions = new ArrayList<>();
        int i=0;
        try {
            List<com.ruoyi.config.JrVersion> minioVersions = minioUtil.getVersion(fileName);
            for (com.ruoyi.config.JrVersion minioVersion : minioVersions) {
                JrVersion version = new JrVersion();
                version.setId(minioVersion.getId());
                version.setFileId(fileId); // 如果需要设置 fileId
                version.setRagDocumentId(minioVersion.getRagDocumentId()); // 版本id
                version.setFileName(minioVersion.getFileName());
                version.setStoragePath(minioVersion.getStoragePath());
                version.setUploadTime(minioVersion.getUploadTime()); // Minio 中没有直接对应的字段
                version.setFileSize(minioVersion.getFileSize());

                version.setVersion(jrVersion.get(i).getVersion()); // 版本号字段
                version.setUploadUserId(jrVersion.get(i).getUploadUserId()); // Minio 中没有直接对应的字段
                version.setMark(jrVersion.get(i).getMark()); // Minio 中没有直接对应的字段
                version.setUpdateUser(jrVersion.get(i).getUpdateUser()); // Minio 中没有直接对应的字段

                versions.add(version);
                i++;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return versions;
    }
}
